package com.example.airmanagement;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.airmanagement.airs.Air_temperature;

import java.util.ArrayList;
import java.util.List;

public class CityCodeActivity extends AppCompatActivity {
    //    储存实体类对象
    private List<Air_temperature> datas = new ArrayList<>();
    private RecyclerView form1;
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.air_activity);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        initDatas();//数据准备
        form1 = findViewById(R.id.form1);//绑定recycleview
        //    指定布局管理器：线性垂直排列
        LinearLayoutManager manager = new LinearLayoutManager(this);
        form1.setLayoutManager(manager);
        //自定义适配器
        adapter = new MyAdapter(datas);
        form1.setAdapter(adapter);
        //    分割线
        form1.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        //    设置左滑删除功能
        setupSwipeToDelete();
        //    设置添加按钮点击事件
        setupAddButtons();
    }



    private void initDatas() {
        datas.add(new Air_temperature("格力", "制冷", 20));
        datas.add(new Air_temperature("海尔", "温暖", 26));
        datas.add(new Air_temperature("美的", "干燥", 19));
    }

    //    设置左滑删除功能
    private void setupSwipeToDelete() {
        ItemTouchHelper.SimpleCallback simpleCallback = new ItemTouchHelper.SimpleCallback(
                0, ItemTouchHelper.LEFT) {
            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
                                  RecyclerView.ViewHolder target) {
                // 不支持拖拽移动，只支持滑动删除
                return false;
            }

            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
                final int position = viewHolder.getAdapterPosition();
                if (position == RecyclerView.NO_POSITION) {
                    return;
                }
                // 先恢复视图，防止自动删除
                adapter.notifyItemChanged(position);
                // 获取要删除的空调信息
                Air_temperature air = adapter.getItem(position);
                if (air != null) {
                    // 显示确认删除对话框
                    showDeleteConfirmDialog(position, air);
                }
            }
        };

        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleCallback);
        itemTouchHelper.attachToRecyclerView(form1);
    }

    //    显示确认删除对话框
    private void showDeleteConfirmDialog(int position, Air_temperature air) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("确认删除");
        builder.setMessage("确定要删除 " + air.getAirName() + " 空调吗？");
        builder.setPositiveButton("删除", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 确认删除
                adapter.removeItem(position);
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 取消删除，恢复视图
                adapter.notifyItemChanged(position);
            }
        });
        builder.setCancelable(false);
        builder.show();
    }

    //    设置添加按钮点击事件
    private void setupAddButtons() {
        Button btnAddGeli = findViewById(R.id.btnAddGeli);
        Button btnAddHaier = findViewById(R.id.btnAddHaier);
        Button btnAddMeidi = findViewById(R.id.btnAddMeidi);

        // 添加格力空调
        btnAddGeli.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addAirConditioner("格力", "制冷", 24);
            }
        });

        // 添加海尔空调
        btnAddHaier.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addAirConditioner("海尔", "温暖", 26);
            }
        });

        // 添加美的空调
        btnAddMeidi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addAirConditioner("美的", "干燥", 22);
            }
        });
    }

    //    添加空调
    private void addAirConditioner(String airName, String tempName, float tempNum) {
        Air_temperature newAir = new Air_temperature(airName, tempName, tempNum);
        adapter.addItem(newAir);
        // 滚动到新添加的项
        form1.smoothScrollToPosition(adapter.getItemCount() - 1);
    }

}